/**
 * 
 */
package de.oszimtcc.timetableview;

import java.util.Date;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.Toast;
import android.widget.ViewFlipper;
import de.oszimtcc.framework.BaseActivity;
import de.oszimtcc.framework.Logger;
import de.oszimtcc.timetable.R;
import de.oszimtcc.timetable.R.id;
import de.oszimtcc.timetable.TimetableApplication;
import de.oszimtcc.timetableadapter.DayAdapter;
import de.oszimtcc.timetablecore.Block;
import de.oszimtcc.timetablecore.Block.BlockType;
import de.oszimtcc.timetablehelper.ViewFlipperGestureDetector;
import de.oszimtcc.timetablescreens.BlockDetailScreen;


/**
 * @author reuter
 *
 */
public class BlockListView extends ListView implements OnItemClickListener,OnTouchListener,OnScrollListener
{

	private BaseActivity activity;
	private GestureDetector gestureDetector;
	private ViewFlipper flipper;
	
	long timeFirst = -1;
	long timeSecond = -1;
	Block lastClickedBlock;
	

	public BlockListView(Context context)
    {
	    super(context);
	    this.setOnTouchListener(this);
    	this.setOnItemClickListener(this);
    	this.setOnScrollListener(this);
    	this.activity = TimetableApplication.getInstance().getActivity();
    }
	
	public BlockListView(Context context, AttributeSet attrs, int defStyle)
    {
	    super(context, attrs, defStyle);
	    this.setOnTouchListener(this);
    	this.setOnItemClickListener(this);
    	this.setOnScrollListener(this);
    	this.activity = TimetableApplication.getInstance().getActivity();
    }


	public BlockListView(Context context, AttributeSet attrs)
    {
	    super(context, attrs);
	    this.setOnTouchListener(this);
    	this.setOnItemClickListener(this);
    	this.setOnScrollListener(this);
    	this.activity = TimetableApplication.getInstance().getActivity();
    }
	
    public void AddContent(TimetableFlipperView pFlipper)
    {
    	this.flipper = pFlipper;
	    gestureDetector = new GestureDetector(new ViewFlipperGestureDetector(activity, flipper));
    }
	
	@Override
    public boolean onTouch(View v, MotionEvent event)
    {
		if(this.flipper != null)
			return gestureDetector.onTouchEvent(event);
		else
			return false;
    }
	
	@Override
    public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount)
    {
		if(this.getAdapter() != null)
		{
			for(int i = getFirstVisiblePosition(); i <= getChildCount(); i++ )
		    {
		    	Block tmp = (Block)((DayAdapter)this.getAdapter()).getBlock(i);
		    	if(tmp != null && tmp.GetHasNewChanges())
		    	{
		    		tmp.SetHasNewChanges(false);
		    	//View v = this.getChildAt(i);
		    	//// Do something fancy with your listitem view
		    		Logger.Error("Animation should start for element # " + i);
		    		ImageView image = ((DayAdapter)this.getAdapter()).getBlockChangePicture(this.getChildAt(i));
		    		this.activity.startAnimationThenHide(image, R.anim.fade_out_slow);
		    	//Animation a =  AnimationUtils.loadAnimation(this.getContext(), R.anim.fade_out_slow);
		    	//image.startAnimation(a);
		    		//this.activity.startAnimationThenHide(R.id.blockHasNewChangePicture, R.anim.fade_out_slow);
		    		//((DayAdapter)this.getAdapter()).getView(i, null, view).findViewById(R.id.blockHasNewChangePicture).startAnimation(AnimationUtils.makeOutAnimation(this.getContext(), true));
		    		
		    	}
		    }
		}
    }

	@Override
    public void onScrollStateChanged(AbsListView view, int scrollState)
    {
	    // TODO Auto-generated method stub
	    
    }
	@Override
    public void onItemClick(AdapterView<?> arg0, View v, int arg2, long arg3)
    {
		Block block = (Block) v.getTag();	
	
		if(timeSecond == -1 && timeFirst != -1)
		{
			timeSecond = new Date().getTime();
			if(timeSecond - timeFirst >= 500 || lastClickedBlock != block) 
			{
				timeFirst = -1;
				timeSecond = -1;
				onItemClick(arg0, v, arg2, arg3);
			} 
		}
		if(timeFirst == -1)
		{
			timeFirst = new Date().getTime();
			lastClickedBlock = block;
		}

		if(timeFirst != -1 && timeSecond != -1)
		{
			if((timeSecond - timeFirst <= 500) && v.getTag() != null) 
			{
				if(block.GetBlockType() != BlockType.Free)
					activity.screen = new BlockDetailScreen(activity,block);
				else
					Toast.makeText(this.getContext(), "NOCH WAS AUSDENKEN WAS HIER PASSIEREN SOLL", Toast.LENGTH_LONG).show();
			} 
			Logger.Error("OSZIMT","Time :" + (timeSecond - timeFirst));
			timeFirst = -1;
			timeSecond = -1;
		}
    }

    public BaseActivity getActivity()
    {
    	return activity;
    }

    public void setActivity(BaseActivity activity)
    {
    	this.activity = activity;
    }
}
